/*
 *接口编写处...
*如果接口需要做Action的权限验证，请在Action上使用属性
*如: [ApiActionPermission("dynamic_option",Enums.ActionPermissionOptions.Search)]
 */
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.Entity.DomainModels;
using VOL.MyTable.IServices;
using VOL.Core.Utilities;
using VOL.Core.Extensions;
using VOL.Core.DBManager;
using Newtonsoft.Json;
using VOL.Core.ManageUser;
using System.Linq;
using VOL.Core.Float;

namespace VOL.MyTable.Controllers
{
    public partial class dynamic_optionController
    {
        private readonly Idynamic_optionService _service;//访问业务代码
        private readonly IHttpContextAccessor _httpContextAccessor;

        [ActivatorUtilitiesConstructor]
        public dynamic_optionController(
            Idynamic_optionService service,
            IHttpContextAccessor httpContextAccessor
        )
        : base(service)
        {
            _service = service;
            _httpContextAccessor = httpContextAccessor;
        }

        /// <summary>
        /// 提交审核,后续再改成通用提交
        /// </summary>
        /// <param name="reimids"></param>
        /// <param name="pdata"></param>
        /// <param name="hq_tousername">汇签的人</param>
        /// <returns></returns>
        [HttpPost, Route("Audit_tj")]//,AllowAnonymous
        public WebResponseContent Audit_tj(string reimids, string pdata,string hq_tousername)
        {
            WebResponseContent webResponse = new WebResponseContent();
            string[] ls_id = reimids.Split(',');
            string[] hq_tousername_array = null;
            if (!string.IsNullOrEmpty(hq_tousername))
                 hq_tousername_array = hq_tousername.Split(',');//汇签人员名单
            List<Guid> ls_list = new List<Guid>();
            foreach (var item in ls_id)
            {
                ls_list.Add((Guid)item.ToGuid());
            }
            
            var dynamic_option_list = DBServerProvider.DbContext.Set<dynamic_option>().Where(s => ls_list.Contains(s.dynamic_option_id)).ToList();
            dynamic queryJson = JsonConvert.DeserializeObject(pdata);//接受前端提交的表单（意见）
            var user = UserContext.Current.GetUserInfo(UserContext.Current.UserId);
            foreach (var item in dynamic_option_list)
            {
               if (!string.IsNullOrEmpty(hq_tousername) && hq_tousername_array.Length > 0)
                {
                    for (int i = 0; i < hq_tousername_array.Length; i++)
                    {
                        FloatManager.add_task(item, (int)item.stepid, hq_tousername_array[i], "汇签");
                    }
                    
                }
                if (queryJson.reyijian.ToString() == "回退")//回退到上一步骤
                {
                    if (!string.IsNullOrEmpty(queryJson.to_stepid.ToString()))//回退到指定的步骤
                    {
                        //int pre_stepid = FloatManager.get_pre_step((int)queryJson.to_stepid, item.dynamic_option_id);
                        FloatManager.Audit_To_Stepid(item, queryJson.remark.ToString(), (int)queryJson.to_stepid);
                    }
                    else
                    {
                        int pre_stepid = FloatManager.get_pre_step((int)item.stepid, item.dynamic_option_id);
                        FloatManager.Audit_To_Stepid(item, queryJson.remark.ToString(), pre_stepid);
                    }
                        
                    
                }else if(queryJson.reyijian.ToString() == "不同意")//回退到起始步骤
                {
                    
                    FloatManager.Audit_To_Stepid(item, queryJson.remark.ToString(), FloatManager.GetFirstStepid(item));
                }
                else
                {
                    FloatManager.Audit(item, queryJson.reyijian.ToString(), queryJson.remark.ToString());//正常审核提交
                }
            };
            return webResponse.OK("操作成功！");
        }
      
    }
}
